home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / delphi / kolekce / d6 / rxlibsetup.exe / {app} / units / PGMNGRED.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-02-19  |  11.7 KB  |  457 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {         Delphi VCL Extensions (RX)                    }
  4. {                                                       }
  5. {         Copyright (c) 2001,2002 SGB Software          }
  6. {         Copyright (c) 1997, 1998 Fedor Koshevnikov,   }
  7. {                        Igor Pavluk and Serge Korolev  }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11.  
  12. unit PgMngrEd;
  13.  
  14. {$I RX.INC}
  15.  
  16. interface
  17.  
  18. uses
  19. {$IFDEF WIN32}
  20.   Windows,
  21. {$ELSE}
  22.   WinTypes, WinProcs,
  23. {$ENDIF}
  24.   SysUtils, Messages, Classes, Graphics, Controls, Forms, Dialogs, Grids,
  25.   RTLConsts, DesignIntf, DesignEditors, VCLEditors, PageMngr, StdCtrls, Placemnt, ExtCtrls,
  26.   VCLUtils, DesignWindows;
  27.  
  28. type
  29.   TProxyEditor = class(TDesignWindow)
  30.     FormStorage: TFormStorage;
  31.     BtnPanel: TPanel;
  32.     CloseBtn: TButton;
  33.     DeleteBtn: TButton;
  34.     ProxyGrid: TDrawGrid;
  35.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  36.     procedure FormShow(Sender: TObject);
  37.     procedure ProxyGridDrawCell(Sender: TObject; Col, Row: Longint; Rect: TRect; State: TGridDrawState);
  38.     procedure ProxyGridSelectCell(Sender: TObject; Col, Row: Longint; var CanSelect: Boolean);
  39.     procedure CloseBtnClick(Sender: TObject);
  40.     procedure DeleteBtnClick(Sender: TObject);
  41.     procedure ProxyGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  42.     procedure FormResize(Sender: TObject);
  43.     procedure FormCreate(Sender: TObject);
  44.   private
  45.     FPageManager: TPageManager;
  46.     FDeleting: Boolean;
  47.     procedure SetPageManager(Value: TPageManager);
  48.     function GetForm: TCustomForm;
  49.     procedure UpdateData;
  50.     function CheckPageManager: Boolean;
  51.     procedure SelectProxy(Proxy: TPageProxy);
  52.     function ProxyByRow(Row: Integer): TPageProxy;
  53.   protected
  54.     function UniqueName(Component: TComponent): string; override;
  55.     procedure Activated; override;
  56.   public
  57.     procedure NameProxy(Sender: TObject);
  58.     procedure ItemsModified(const Designer: IDesigner); override;
  59.     procedure DesignerClosed(const Designer: IDesigner; AGoingDormant: Boolean); override;
  60.     function GetEditState: TEditState; override;
  61.     procedure ItemDeleted(const ADesigner: IDesigner; Item: TPersistent); override;
  62.     property PageManager: TPageManager read FPageManager write SetPageManager;
  63.     property OwnerForm: TCustomForm read GetForm;
  64.   end;
  65.  
  66. { TProxyListProperty }
  67.  
  68.   TProxyListProperty = class(TPropertyEditor)
  69.     function GetAttributes: TPropertyAttributes; override;
  70.     function GetValue: string; override;
  71.     procedure Edit; override;
  72.   end;
  73.  
  74. { TPageManagerEditor }
  75.  
  76.   TPageManagerEditor = class(TComponentEditor)
  77.     procedure ExecuteVerb(Index: Integer); override;
  78.     function GetVerb(Index: Integer): string; override;
  79.     function GetVerbCount: Integer; override;
  80.   end;
  81.  
  82. { TPageNameProperty }
  83.  
  84.   TPageNameProperty = class(TStringProperty)
  85.     function GetAttributes: TPropertyAttributes; override;
  86.     procedure GetValues(Proc: TGetStrProc); override;
  87.   end;
  88.  
  89. { TPageBtnProperty }
  90.  
  91.   TPageBtnProperty = class(TComponentProperty)
  92.     procedure GetValues(Proc: TGetStrProc); override;
  93.   end;
  94.  
  95. implementation
  96.  
  97. uses Consts, Buttons, RxCtrls, RXConst, RXLConst, RxDsgn;
  98.  
  99. {$R *.DFM}
  100.  
  101. {$IFDEF WIN32}
  102.  {$D-}
  103. {$ENDIF}
  104.  
  105. {$IFDEF RX_D4}
  106. type
  107.   TDesigner = IDesigner;
  108.   TFormDesigner = IDesigner;
  109. {$ENDIF}
  110.  
  111. function FindEditor(Manager: TPageManager): TProxyEditor;
  112. var
  113.   I: Integer;
  114. begin
  115.   Result := nil;
  116.   for I := 0 to Screen.FormCount - 1 do begin
  117.     if Screen.Forms[I] is TProxyEditor then begin
  118.       if TProxyEditor(Screen.Forms[I]).PageManager = Manager then
  119.       begin
  120.         Result := TProxyEditor(Screen.Forms[I]);
  121.         Break;
  122.       end;
  123.     end;
  124.   end;
  125. end;
  126.  
  127. procedure ShowProxyEditor(Designer: TDesigner; Manager: TPageManager);
  128. var
  129.   Editor: TProxyEditor;
  130. begin
  131.   if Manager = nil then Exit;
  132.   Editor := FindEditor(Manager);
  133.   if Editor <> nil then begin
  134.     Editor.Show;
  135.     if Editor.WindowState = wsMinimized then Editor.WindowState := wsNormal;
  136.   end
  137.   else begin
  138.     Editor := TProxyEditor.Create(Application);
  139.     try
  140.       Editor.Designer := TFormDesigner(Designer);
  141.       Editor.PageManager := Manager;
  142.       Editor.Show;
  143.     except
  144.       Editor.Free;
  145.       raise;
  146.     end;
  147.   end;
  148. end;
  149.  
  150. { TProxyListProperty }
  151.  
  152. function TProxyListProperty.GetAttributes: TPropertyAttributes;
  153. begin
  154.   Result := [paDialog, paReadOnly];
  155. end;
  156.  
  157. function TProxyListProperty.GetValue: string;
  158. var
  159.   List: TList;
  160. begin
  161.   List := TList(Pointer(GetOrdValue));
  162.   if (List = nil) or (List.Count = 0) then
  163.     Result := ResStr(srNone)
  164.   else FmtStr(Result, '(%s)', [GetPropType^.Name]);
  165. end;
  166.  
  167. procedure TProxyListProperty.Edit;
  168. begin
  169.   ShowProxyEditor(Designer, TPageManager(GetComponent(0)));
  170. end;
  171.  
  172. { TPageBtnProperty }
  173.  
  174. procedure TPageBtnProperty.GetValues(Proc: TGetStrProc);
  175. var
  176.   I: Integer;
  177.   Component: TComponent;
  178. begin
  179.   for I := 0 to Designer.Root.ComponentCount - 1 do begin
  180.     Component := Designer.Root.Components[I];
  181.     if (Component.InheritsFrom(TButtonControl) or 
  182.       Component.InheritsFrom(TSpeedButton) or 
  183.       Component.InheritsFrom(TRxSpeedButton)) and 
  184.       (Component.Name <> '') then Proc(Component.Name);
  185.   end;
  186. end;
  187.  
  188. { TPageNameProperty }
  189.  
  190. function TPageNameProperty.GetAttributes: TPropertyAttributes;
  191. begin
  192.   Result := [paValueList];
  193. end;
  194.  
  195. procedure TPageNameProperty.GetValues(Proc: TGetStrProc);
  196. var
  197.   PageProxy: TPageProxy;
  198.   I: Integer;
  199. begin
  200.   PageProxy := GetComponent(0) as TPageProxy;
  201.   if (PageProxy <> nil) and (PageProxy.PageManager <> nil) and
  202.     (PageProxy.PageManager.PageOwner <> nil) then
  203.   begin
  204.     for I := 0 to PageProxy.PageManager.PageCount - 1 do begin
  205.       Proc(PageProxy.PageManager.PageNames[I]);
  206.     end;
  207.   end;
  208. end;
  209.  
  210. { TPageManagerEditor }
  211.  
  212. procedure TPageManagerEditor.ExecuteVerb(Index: Integer);
  213. begin
  214.   case Index of
  215.     0: ShowProxyEditor(Designer, TPageManager(Component));
  216.   end;
  217. end;
  218.  
  219. function TPageManagerEditor.GetVerb(Index: Integer): string;
  220. begin
  221.   case Index of
  222.     0: Result := LoadStr(srProxyEditor);
  223.   end;
  224. end;
  225.  
  226. function TPageManagerEditor.GetVerbCount: Integer;
  227. begin
  228.   Result := 1;
  229. end;
  230.  
  231. { TProxyEditor }
  232.  
  233. procedure TProxyEditor.SetPageManager(Value: TPageManager);
  234. begin
  235.   if FPageManager <> Value then begin
  236.     if FPageManager <> nil then FPageManager.OnCheckProxy := nil;
  237.     FPageManager := Value;
  238.     if FPageManager <> nil then FPageManager.OnCheckProxy := NameProxy;
  239.     UpdateData;
  240.   end;
  241. end;
  242.  
  243. function TProxyEditor.UniqueName(Component: TComponent): string;
  244. var
  245.   Temp: string;
  246. {$IFNDEF WIN32}
  247.   I: Integer;
  248.   Comp: TComponent;
  249. {$ENDIF}
  250. begin
  251.   Result := '';
  252.   if (Component <> nil) then Temp := Component.ClassName
  253.   else Temp := TPageProxy.ClassName;
  254.   if (UpCase(Temp[1]) = 'T') and (Length(Temp) > 1) then
  255.     System.Delete(Temp, 1, 1);
  256. {$IFDEF WIN32}
  257.   Result := Designer.UniqueName(Temp);
  258. {$ELSE}
  259.   I := 1;
  260.   repeat
  261.     Result := Temp + IntToStr(I);
  262.     Comp := OwnerForm.FindComponent(Result);
  263.     Inc(I);
  264.   until (Comp = nil) or (Comp = Component);
  265. {$ENDIF}
  266. end;
  267.  
  268. function TProxyEditor.GetEditState: TEditState;
  269. begin
  270.   Result := [];
  271. end;
  272.  
  273. procedure TProxyEditor.NameProxy(Sender: TObject);
  274. begin
  275.   if (Sender is TPageProxy) and (TPageProxy(Sender).Name = '') then
  276.     TPageProxy(Sender).Name := UniqueName(TPageProxy(Sender));
  277. end;
  278.  
  279. procedure TProxyEditor.DesignerClosed(const Designer: IDesigner; AGoingDormant: Boolean);
  280. begin
  281.   if Designer.Root = OwnerForm then Free;
  282. end;
  283.  
  284. procedure TProxyEditor.ItemsModified(const Designer: IDesigner);
  285. begin
  286.   if not (csDestroying in ComponentState) then UpdateData;
  287. end;
  288.  
  289. procedure TProxyEditor.Activated;
  290. begin
  291.   SelectProxy(ProxyByRow(ProxyGrid.Row - 1));
  292. end;
  293.  
  294. procedure TProxyEditor.ItemDeleted(const ADesigner: IDesigner; Item: TPersistent);
  295. begin
  296.   if Item = FPageManager then begin
  297.     FPageManager := nil;
  298.     Close;
  299.   end;
  300. end;
  301.  
  302. procedure TProxyEditor.UpdateData;
  303. var
  304.   ProxyCount: Integer;
  305. begin
  306.   if CheckPageManager then begin
  307.     if not FDeleting then FPageManager.Resync;
  308.     ProxyCount := FPageManager.PageProxies.Count;
  309.     if ProxyCount = 0 then begin
  310.       ProxyGrid.RowCount := 2;
  311.       SelectProxy(nil);
  312.     end
  313.     else begin
  314.       ProxyGrid.RowCount := 1 + ProxyCount;
  315.     end;
  316.     DeleteBtn.Enabled := ProxyCount > 0;
  317.     ProxyGrid.Invalidate;
  318.   end;
  319. end;
  320.  
  321. function TProxyEditor.GetForm: TCustomForm;
  322. begin
  323.   Result := GetParentForm(BtnPanel); //Designer.Form;
  324. end;
  325.  
  326. procedure TProxyEditor.FormClose(Sender: TObject; var Action: TCloseAction);
  327. begin
  328.   Action := caFree;
  329.   if FPageManager <> nil then FPageManager.OnCheckProxy := nil;
  330. end;
  331.  
  332. procedure TProxyEditor.FormShow(Sender: TObject);
  333. begin
  334.   if FPageManager.PageOwner <> nil then begin
  335.     Caption := Format(LoadStr(srPageProxies), [FPageManager.PageOwner.Name]);
  336.   end;
  337. end;
  338.  
  339. function TProxyEditor.CheckPageManager: Boolean;
  340. begin
  341.   Result := (FPageManager <> nil) and (FPageManager.Owner <> nil) and
  342.     (Designer.Root <> nil);
  343. end;
  344.  
  345. procedure TProxyEditor.SelectProxy(Proxy: TPageProxy);
  346. var
  347.   FComponents: IDesignerSelections;
  348. begin
  349.   if CheckPageManager and Active then begin
  350.     FComponents := CreateSelectionList;
  351.     if Proxy <> nil then
  352.       FComponents.Add(Proxy)
  353.     else
  354.       FComponents.Add(FPageManager);
  355.     SetSelection(FComponents);
  356.   end;
  357. end;
  358.  
  359. function TProxyEditor.ProxyByRow(Row: Integer): TPageProxy;
  360. begin
  361.   Result := nil;
  362.   if CheckPageManager and (Row >= 0) and
  363.     (Row < FPageManager.PageProxies.Count) then
  364.   begin
  365.     Result := FPageManager.PageProxies.Items[Row];
  366.   end;
  367. end;
  368.  
  369. procedure TProxyEditor.ProxyGridDrawCell(Sender: TObject; Col,
  370.   Row: Longint; Rect: TRect; State: TGridDrawState);
  371. var
  372.   CellText: string;
  373.   Proxy: TPageProxy;
  374. begin
  375.   CellText := '';
  376.   if gdFixed in State then begin
  377.     case Col of
  378.       0: CellText := LoadStr(srProxyName);
  379.       1: CellText := LoadStr(srPageName);
  380.     end;
  381.   end
  382.   else begin
  383.     Proxy := ProxyByRow(Row - 1);
  384.     if Proxy <> nil then begin
  385.       case Col of
  386.         0: CellText := Proxy.Name;
  387.         1: CellText := Proxy.PageName;
  388.       end;
  389.     end;
  390.   end;
  391.   DrawCellText(ProxyGrid, Col, Row, CellText, Rect, taLeftJustify, vaCenter);
  392. end;
  393.  
  394. procedure TProxyEditor.ProxyGridSelectCell(Sender: TObject; Col,
  395.   Row: Longint; var CanSelect: Boolean);
  396. begin
  397.   SelectProxy(ProxyByRow(Row - 1));
  398. end;
  399.  
  400. procedure TProxyEditor.CloseBtnClick(Sender: TObject);
  401. begin
  402.   Close;
  403. end;
  404.  
  405. procedure TProxyEditor.DeleteBtnClick(Sender: TObject);
  406. var
  407.   Proxy: TPageProxy;
  408. begin
  409.   Proxy := ProxyByRow(ProxyGrid.Row - 1);
  410.   if Proxy <> nil then begin
  411.     Self.ValidateRename(Proxy, Proxy.Name, '');
  412.     FDeleting := True;
  413.     try
  414.       Proxy.Free;
  415.       Designer.Modified;
  416.     finally
  417.       FDeleting := False;
  418.     end;
  419.   end;
  420. end;
  421.  
  422. procedure TProxyEditor.ProxyGridKeyDown(Sender: TObject; var Key: Word;
  423.   Shift: TShiftState);
  424. begin
  425.   if Shift = [] then begin
  426.     case Key of
  427.       VK_RETURN:
  428.         if ProxyByRow(ProxyGrid.Row - 1) <> nil then begin
  429.           ActivateInspector(#0);
  430.         end;
  431.       VK_DELETE:
  432.         DeleteBtnClick(nil);
  433.     end;
  434.   end;
  435. end;
  436.  
  437. procedure TProxyEditor.FormResize(Sender: TObject);
  438. begin
  439.   with ProxyGrid do begin
  440.     DefaultColWidth := (ClientWidth - 1) div 2;
  441.     ColWidths[1] := ClientWidth - ColWidths[0] - 1;
  442.   end;
  443. end;
  444.  
  445. procedure TProxyEditor.FormCreate(Sender: TObject);
  446. begin
  447.   if NewStyleControls then Font.Style := [];
  448. {$IFDEF WIN32}
  449.   with FormStorage do begin
  450.     UseRegistry := True;
  451.     IniFileName := SDelphiKey;
  452.   end;
  453. {$ENDIF}
  454. end;
  455.  
  456. end.
  457.